package com.sanjiahantang.youjiarole.module.programmeprice.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.views.MyListView;
import com.sanjiahantang.uilibrary.views.TagGroup;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.config.StaticValue;
import com.sanjiahantang.youjiarole.module.programmeprice.beans.ProgramBean;
import com.sanjiahantang.youjiarole.utils.FileUtils;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zy on 2017/8/9.
 */

public class ProgramBeSureAdapter extends RecyclerView.Adapter implements StaticValue {


    private final Context context;
    //布局标识集合
    private final List<Integer> typeList;
    private LayoutInflater inflater;
    private ProgramBean.Data data;
    private Boolean aBoolean = false;
    private BaseView.OnLoadListener loadListener;
    private QuickAdapter<ProgramBean.Data.ServiceListBean> SGAdapter;
    private QuickAdapter<ProgramBean.Data.GoodsListBean> ZCAdapter;
    private QuickAdapter<ProgramBean.Data.GoodsListBean> JJAdapter;
    private QuickAdapter<ProgramBean.Data.GoodsListBean> PSAdapter;

    public ProgramBean.Data getData() {
        return data;
    }

    public void setData(ProgramBean.Data data) {
        this.data = data;
    }

    public static final int TYPE_SHIGONG = 2;//施工
    public static final int TYPE_ZHUCAI = 3;
    public static final int TYPE_JIAJU = 4;
    public static final int TYPE_PEISHI = 5;

    public ProgramBeSureAdapter(Context context, BaseView.OnLoadListener listener) {
        this.context = context;
        this.typeList = new ArrayList<>();
        typeList.add(TYPE_SHIGONG);
        typeList.add(TYPE_ZHUCAI);
        typeList.add(TYPE_JIAJU);
        typeList.add(TYPE_PEISHI);
        this.loadListener = listener;
        this.inflater = LayoutInflater.from(context);
    }

    @Override
    public ProgramHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        int resId = -1;
        switch (viewType) {
            case TYPE_SHIGONG:
                resId = R.layout.item_program_nopic;
                break;
            case TYPE_ZHUCAI:
                resId = R.layout.item_program_nopic;
                break;
            case TYPE_JIAJU:
                resId = R.layout.item_program_nopic;
                break;
            case TYPE_PEISHI:
                resId = R.layout.item_program_nopic;
                break;

        }
        return new ProgramHolder(inflater.inflate(resId, null), data, viewType);

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((ProgramHolder) holder).bindData(data, typeList.get(position));
    }

    @Override
    public int getItemViewType(int position) {
        return typeList.get(position);
    }

    @Override
    public int getItemCount() {
        return typeList.size();
    }

    private class ProgramHolder extends RecyclerView.ViewHolder {

        public ProgramHolder(View itemView, ProgramBean.Data data, int position) {
            super(itemView);
            bindData(data, position);
        }

        private void bindData(ProgramBean.Data data, int position) {

            if (position == TYPE_SHIGONG) {
                //施工列表
                initShigong(itemView, data.getServiceList());
            }
            if (position == TYPE_ZHUCAI) {
                //主材列表
                initZhucai(itemView, data.getMaterialList());
            }

            if (position == TYPE_JIAJU) {
                initJiaju(itemView, data.getFurnitureList());
            }

            if (position == TYPE_PEISHI) {
                initPeishi(itemView, data.getAccessoriesList());
            }
        }

        //施工
        public void initShigong(View itemView, final List<ProgramBean.Data.ServiceListBean> sgData) {

            final CheckBox sg_checkbox = (CheckBox) itemView.findViewById(R.id.iv_titleIcon);
            final MyListView goodListView = (MyListView) itemView.findViewById(R.id.SG_list);
            TextView txtGoodsCount = (TextView) itemView.findViewById(R.id.comName);
            TextView TotalCount = (TextView) itemView.findViewById(R.id.TotalCount);
            TextView TotalPrice = (TextView) itemView.findViewById(R.id.TotalPrice);
            final LinearLayout countItem = (LinearLayout) itemView.findViewById(R.id.countItem);
            txtGoodsCount.setText("施工");
            int count = 0;
            float price = 0f;
            for (ProgramBean.Data.ServiceListBean item : sgData) {
                count += item.getServiceNum();
                price += Float.parseFloat(item.getServiceCount());
            }
            TotalCount.setText("" + count);
            TotalPrice.setText("¥" + price);
            SGAdapter = new QuickAdapter<ProgramBean.Data.ServiceListBean>(context, R.layout.item_program_nopic_n, sgData) {
                @Override
                protected void convert(BaseAdapterHelper helper, ProgramBean.Data.ServiceListBean item) {
                    helper.setText(R.id.sg_name, item.getServiceName());
                    helper.setText(R.id.txtPrice, "¥" + item.getServicePrice());
                    helper.setText(R.id.txtCount, "x" + item.getServiceNum());
                    countItem.setVisibility(View.VISIBLE);

                    ToatalMoney();
                }

            };
            goodListView.setAdapter(SGAdapter);
        }

        //主材
        private void initZhucai(View itemView, final List<ProgramBean.Data.GoodsListBean> zcData) {

            final CheckBox zc_checkbox = (CheckBox) itemView.findViewById(R.id.iv_titleIcon);
            MyListView aclistView = (MyListView) itemView.findViewById(R.id.SG_list);
            TextView txtGoodsCount = (TextView) itemView.findViewById(R.id.comName);
            TextView TotalCount = (TextView) itemView.findViewById(R.id.TotalCount);
            TextView TotalPrice = (TextView) itemView.findViewById(R.id.TotalPrice);
            final LinearLayout countItem = (LinearLayout) itemView.findViewById(R.id.countItem);
            int count = 0;
            float price = 0f;
            for (ProgramBean.Data.GoodsListBean item : zcData) {
                count += item.getGoodsNum();
                price += Float.parseFloat(item.getGoodsCount());
            }
            TotalCount.setText("" + count);
            TotalPrice.setText("¥" + price);
            txtGoodsCount.setText("主材");
            ZCAdapter = new QuickAdapter<ProgramBean.Data.GoodsListBean>(context, R.layout.item_program_nopic_p, zcData) {
                @Override
                protected void convert(final BaseAdapterHelper helper, final ProgramBean.Data.GoodsListBean item) {

                    helper.setText(R.id.txtMainName, item.getGoodsName());
                    helper.setText(R.id.txtMainPrice, "¥" + item.getGoodsPrice());
                    TagGroup tagGroup = helper.getView(R.id.txtName);
                    tagGroup.setTags(item.getBrandType());
                    helper.setText(R.id.txtCount, "" + item.getGoodsNum());
                    ImageView nearby_shop_img = helper.getView(R.id.iv_pic);
                    Picasso.with(context).load(FileUtils.rePath(item.getUrl())).placeholder(RES_ID_GOODS_DEFAULT).into(nearby_shop_img);
                    String material = "" + item.getIsRecommend();
                    final CheckBox imageView = helper.getView(R.id.iv_icon);//子选中项
                    countItem.setVisibility(View.VISIBLE);
                    ToatalMoney();
                }
            };
            aclistView.setAdapter(ZCAdapter);
        }

        //配饰
        private void initPeishi(View itemView, final List<ProgramBean.Data.GoodsListBean> psData) {

            final CheckBox ps_checkbox = (CheckBox) itemView.findViewById(R.id.iv_titleIcon);
            MyListView psListView = (MyListView) itemView.findViewById(R.id.SG_list);
            TextView txtGoodsCount = (TextView) itemView.findViewById(R.id.comName);
            final LinearLayout linearCom = (LinearLayout) itemView.findViewById(R.id.comAdd);
            final TextView line = (TextView) itemView.findViewById(R.id.line);
            TextView TotalCount = (TextView) itemView.findViewById(R.id.TotalCount);
            TextView TotalPrice = (TextView) itemView.findViewById(R.id.TotalPrice);
            final LinearLayout countItem = (LinearLayout) itemView.findViewById(R.id.countItem);
            int count = 0;
            float price = 0f;
            for (ProgramBean.Data.GoodsListBean item : psData) {
                count += item.getGoodsNum();
                price += Float.parseFloat(item.getGoodsCount());
            }
            TotalCount.setText("" + count);
            TotalPrice.setText("¥" + price);
            txtGoodsCount.setText("配饰");
            PSAdapter = new QuickAdapter<ProgramBean.Data.GoodsListBean>(context, R.layout.item_program_nopic_y, psData) {
                @Override
                protected void convert(final BaseAdapterHelper helper, final ProgramBean.Data.GoodsListBean item) {
                    helper.setText(R.id.txtMainName, item.getGoodsName());
                    helper.setText(R.id.txtMainPrice, "¥" + item.getGoodsPrice());
                    helper.setText(R.id.txtCount, "" + item.getGoodsNum());
                    TagGroup tagGroup = helper.getView(R.id.txtName);
                    tagGroup.setTags(item.getBrandType());
                    LinearLayout isfirst = helper.getView(R.id.isFirst);
                    LinearLayout issecond = helper.getView(R.id.isSecond);
                    ImageView nearby_shop_img = helper.getView(R.id.iv_pic);
                    final TextView tv_number = helper.getView(R.id.choice_number);
                    Picasso.with(context).load(FileUtils.rePath(item.getUrl())).placeholder(RES_ID_GOODS_DEFAULT).into(nearby_shop_img);
                    countItem.setVisibility(View.VISIBLE);

                }
            };
            psListView.setAdapter(PSAdapter);

        }

        //家居
        private void initJiaju(View itemView, final List<ProgramBean.Data.GoodsListBean> jjData) {

            final CheckBox JJ_checkbox = (CheckBox) itemView.findViewById(R.id.iv_titleIcon);
            final MyListView JJListView = (MyListView) itemView.findViewById(R.id.SG_list);
            TextView txtGoodsCount = (TextView) itemView.findViewById(R.id.comName);
            final LinearLayout linearCom = (LinearLayout) itemView.findViewById(R.id.comAdd);
            final TextView line = (TextView) itemView.findViewById(R.id.line);
            TextView TotalCount = (TextView) itemView.findViewById(R.id.TotalCount);
            TextView TotalPrice = (TextView) itemView.findViewById(R.id.TotalPrice);
            final LinearLayout countItem = (LinearLayout) itemView.findViewById(R.id.countItem);
            int count = 0;
            float price = 0f;
            for (ProgramBean.Data.GoodsListBean item : jjData) {
                count += item.getGoodsNum();
                price += Float.parseFloat(item.getGoodsCount());
            }
            TotalCount.setText("" + count);
            TotalPrice.setText("¥" + price);
            txtGoodsCount.setText("家具");
            JJAdapter = new QuickAdapter<ProgramBean.Data.GoodsListBean>(context, R.layout.item_program_nopic_y, jjData) {
                @Override
                protected void convert(final BaseAdapterHelper helper, final ProgramBean.Data.GoodsListBean item) {
                    helper.setText(R.id.txtMainName, item.getGoodsName());
                    helper.setText(R.id.txtMainPrice, "¥" + item.getGoodsPrice());
                    helper.setText(R.id.txtCount, "" + item.getGoodsNum());
                    TagGroup tagGroup = helper.getView(R.id.txtName);
                    tagGroup.setTags(item.getBrandType());
                    ImageView nearby_shop_img = helper.getView(R.id.iv_pic);
                    Picasso.with(context).load(FileUtils.rePath(item.getUrl())).placeholder(RES_ID_GOODS_DEFAULT).into(nearby_shop_img);
                    final CheckBox iv_icon = helper.getView(R.id.iv_icon);
                    countItem.setVisibility(View.VISIBLE);

                }
            };
            JJListView.setAdapter(JJAdapter);
        }

//
//        private void bindInvoice(View itemView, final CreateOrderPost.OrderInvoiceInfoDtoBean invoice) {
//            if(invoice == null){
//                itemView.findViewById(R.id.rlNoInvoice).setVisibility(View.VISIBLE);
//                itemView.findViewById(R.id.rlInvoiceInfo).setVisibility(View.GONE);
//            }else {
//                itemView.findViewById(R.id.rlNoInvoice).setVisibility(View.GONE);
//                itemView.findViewById(R.id.rlInvoiceInfo).setVisibility(View.VISIBLE);
//
//                TextView txtInvoiceTitle = itemView.findViewById(R.id.txtInvoiceTitle);
//                TextView txtInvoiceTaxNo = itemView.findViewById(R.id.txtInvoiceTaxNo);
//                if (!TextUtils.isEmpty(invoice.getTaxNum())){
//                    itemView.findViewById(R.id.llInvoiceTaxNo).setVisibility(View.VISIBLE);
//                    txtInvoiceTaxNo.setText(invoice.getTaxNum());
//                }else {
//                    itemView.findViewById(R.id.llInvoiceTaxNo).setVisibility(View.GONE);
//                }
//                txtInvoiceTitle.setText(invoice.getInvoiceTitle());
//            }
//
//            if (orderInfo == null || orderInfo.getStatusId() == OrderView.TYPE_UN_PAY){
//                itemView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        Activity activity = (Activity) context;
//                        Intent mIntent = new Intent(activity, InvoiceActivity.class);
//                        if(invoice != null){
//                            if(!TextUtils.isEmpty(invoice.getTaxNum())){
//                                mIntent.putExtra(InvoiceView.KEY_TAX_NO,invoice.getTaxNum());
//                            }
//                            mIntent.putExtra(InvoiceView.KEY_TITLE,invoice.getInvoiceTitle());
//                        }
//                        activity.startActivityForResult(mIntent, OrderView.INTENT_INVOICE);
//                    }
//                });
//                itemView.findViewById(R.id.arrowInvoice1).setVisibility(View.VISIBLE);
//                itemView.findViewById(R.id.arrowInvoice2).setVisibility(View.VISIBLE);
//            }else{
//                itemView.findViewById(R.id.arrowInvoice1).setVisibility(View.GONE);
//                itemView.findViewById(R.id.arrowInvoice2).setVisibility(View.GONE);
//            }
//
//        }
//
//        private void bindConsumerMessage(View itemView, String message) {
//            EditText etConsumerMessage = itemView.findViewById(R.id.etConsumerMessage);
//            if (!TextUtils.isEmpty(message)){
//                etConsumerMessage.setText(message);
//            }
//
//            if (orderInfo != null && orderInfo.getStatusId() != OrderView.TYPE_UN_PAY){
//                etConsumerMessage.setEnabled(false);
//            }
//
//            etConsumerMessage.addTextChangedListener(new TextWatcher() {
//                @Override
//                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
//
//                }
//
//                @Override
//                public void onTextChanged(CharSequence s, int start, int before, int count) {
//                    consumerMessage = s.toString();
//                }
//
//                @Override
//                public void afterTextChanged(Editable s) {
//
//                }
//            });
//        }

    }

    public void ToatalMoney() {
        float sgPrice = 0;
        float zcPrice = 0;
        float jjPrice = 0;
        float psPrice = 0;
        //施工总金额
        if (ProgramBeSureAdapter.this.data.getServiceList() != null) {
            for (int i = 0; i < ProgramBeSureAdapter.this.data.getServiceList().size(); i++) {
                if (ProgramBeSureAdapter.this.data.getServiceList().get(i).getIsRecommend() == 1) {
                    sgPrice += Float.parseFloat(ProgramBeSureAdapter.this.data.getServiceList().get(i).getServicePrice()) *
                            Float.valueOf(ProgramBeSureAdapter.this.data.getServiceList().get(i).getServiceNum());
                }
            }
        }
        //资材总金额
        if (ProgramBeSureAdapter.this.data.getMaterialList() != null) {
            for (int i = 0; i < ProgramBeSureAdapter.this.data.getMaterialList().size(); i++) {
                if (ProgramBeSureAdapter.this.data.getMaterialList().get(i).getIsRecommend() == 1) {
                    zcPrice += Float.parseFloat(ProgramBeSureAdapter.this.data.getMaterialList().get(i).getGoodsPrice()) * Float.valueOf(ProgramBeSureAdapter.this.data.getMaterialList().get(i).getGoodsNum());
                }
            }
        }
        //家居总金额
        if (ProgramBeSureAdapter.this.data.getFurnitureList() != null) {
            for (int i = 0; i < ProgramBeSureAdapter.this.data.getFurnitureList().size(); i++) {
                if (ProgramBeSureAdapter.this.data.getFurnitureList().get(i).getIsRecommend() == 1) {
                    jjPrice += Float.parseFloat(ProgramBeSureAdapter.this.data.getFurnitureList().get(i).getGoodsPrice()) * Float.valueOf(ProgramBeSureAdapter.this.data.getFurnitureList().get(i).getGoodsNum());
                }
            }
        }
        //配飾总金额
        if (ProgramBeSureAdapter.this.data.getAccessoriesList() != null) {
            for (int i = 0; i < ProgramBeSureAdapter.this.data.getAccessoriesList().size(); i++) {
                if (ProgramBeSureAdapter.this.data.getAccessoriesList().get(i).getIsRecommend() == 1) {
                    psPrice += Float.parseFloat(ProgramBeSureAdapter.this.data.getAccessoriesList().get(i).getGoodsPrice()) * Float.valueOf(ProgramBeSureAdapter.this.data.getAccessoriesList().get(i).getGoodsNum());
                }
            }
        }
        Object[] objects = new Object[2];
        objects[0] = CALLBACK_TYPE_QUICK_ADAPTER;
        objects[1] = sgPrice + zcPrice + jjPrice + psPrice;
        loadListener.onLoad(objects);
    }

}
